 //
//  ScrollToTopHelper.m
//  Weiyitemai
//
//  Created by Ron on 8/16/14.
//  Copyright (c) 2014 HGG. All rights reserved.
//

#import "ScrollToTopHelper.h"
#import <objc/runtime.h>

static void *scrollToTopHelperPropertyKey = &scrollToTopHelperPropertyKey;

@implementation UIScrollView(ScrollToTopHelper)

- (ScrollToTopHelper*)scrollToTopHelper
{
    return objc_getAssociatedObject(self, scrollToTopHelperPropertyKey);
}

- (void)setScrollToTopHelper:(NSObject *)value {
    objc_setAssociatedObject(self, scrollToTopHelperPropertyKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

@interface ScrollToTopHelper()

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, assign) CGFloat visibilityOffset;
@property (nonatomic, assign) CGFloat bottomEdge;

@end

@implementation ScrollToTopHelper

- (void)dealloc{
    
}

+ (ScrollToTopHelper*)setupOnScrollview:(UIScrollView*)scrollView withVisibilityOffset:(CGFloat)offset bottomEdge:(CGFloat)bottomEdge partyView:(UIView *)superView{
    
    NSCAssert(scrollView.superview, @"must has superView");
    ScrollToTopHelper *helper = [[ScrollToTopHelper alloc] init];
    helper.scrollView = scrollView;
    helper.superView = superView;
    helper.visibilityOffset = MAX(scrollView.height, offset);
    helper.bottomEdge = bottomEdge;
    [helper setup];
    return helper;
}

- (UIButton*)scrollToTopButton{
    if (!_scrollToTopButton) {
        _scrollToTopButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _scrollToTopButton.exclusiveTouch = YES;
        [_scrollToTopButton setImage:[UIImage imageNamed:@"button_scrolltotop"] forState:UIControlStateNormal];
//        [self.scrollView.superview insertSubview:_scrollToTopButton aboveSubview:self.scrollView];
        _scrollToTopButton.translatesAutoresizingMaskIntoConstraints = NO;
        [self.superView addSubview:_scrollToTopButton];
        CGFloat bottomEdge = self.bottomEdge;
        if (bottomEdge <= 0) {
            bottomEdge = 59;
        }
//        UIView *superView = self.scrollView.superview;
        _scrollToTopButton.frame = CGRectMake(self.superView.frame.size.width-55, self.superView.frame.size.height-bottomEdge, 40, 40);
//        [_scrollToTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.scrollView.superview).offset(bottomEdge);
//            make.trailing.equalTo(self.scrollView.superview).offset(15);
//            make.width.equalTo(@40);
//            make.height.equalTo(@40);
//        }];
//        [_scrollToTopButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:bottomEdge];
//        [_scrollToTopButton autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:15];
    }
    return _scrollToTopButton;
}

- (void)onClickButton:(id)sender{
    [self.scrollView setContentOffset:CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];
}

- (void)setup{
    [self addObserver];
    [self.scrollToTopButton addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)cleanUp{
    [self removeObserver];
}

- (void)addObserver{
    [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)removeObserver{
    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"contentOffset"])
    {
        CGPoint newContentOffset = [[change valueForKey:NSKeyValueChangeNewKey] CGPointValue];
        if (newContentOffset.y < self.visibilityOffset){
            self.scrollToTopButton.hidden = YES;
        }else {
            self.scrollToTopButton.hidden = NO;
        }
    
    }
}



@end



